/*
* SuctionController.java
* Created on  2015/7/21 10:26
* 版本       修改时间          作者      修改内容
* V1.0.1    2015/7/21       longshihui    初始版本
*
*/

package com.infitecs.eyas.suction;

import com.infitecs.eyas.boiler.model.AirGasParameter;
import com.infitecs.eyas.boiler.model.Efficiency;
import com.infitecs.eyas.boiler.service.AirGasParameterService;
import com.infitecs.eyas.boiler.service.EfficiencyService;
import com.infitecs.eyas.combustion.model.CalculateBurn;
import com.infitecs.eyas.combustion.service.BurnService;
import com.infitecs.eyas.power.dao.OperateFieldDao;
import com.infitecs.eyas.power.dao.PowerDao;
import com.infitecs.eyas.power.model.OperateField;
import com.infitecs.eyas.power.model.Power;
import com.infitecs.eyas.result.model.MiddleResult;
import com.infitecs.eyas.result.service.MiddleResultService;
import com.infitecs.eyas.step.service.StepService;
import com.infitecs.eyas.suction.model.SuctionDes;
import com.infitecs.eyas.suction.service.SuctionDesService;
import com.infitecs.eyas.session.SessionService;
import com.infitecs.krill.ajax.response.AjaxResponse;
import com.infitecs.krill.ajax.response.ReturnState;
import com.infitecs.krill.exception.ValidateErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 类的描述信息
 *
 * @author longshihui
 * @version 1.0.1
 */
@Controller
@RequestMapping("/suction/design")
public class SuctionDesController {

    @Autowired
    private SessionService sessionService;

    @Autowired
    private SuctionDesService suctionDesService;

    @Autowired
    private BurnService burnService;

    @Autowired
    private EfficiencyService efficiencyService;

    @Autowired
    private AirGasParameterService airGasParameterService;

    @Autowired
    private MiddleResultService middleResultService;
    @Autowired
    private StepService stepService;
    @Autowired
    private OperateFieldDao operateFieldDao;
    @Autowired
    private PowerDao powerDao;

    @RequestMapping("")
    public String getPage(HttpServletRequest request) {
        String cId = (String) request.getSession().getAttribute("caseId");
        if (cId == null) {
            return "suction/design";
        }
        int caseId = Integer.parseInt(cId);
        int step = stepService.getMzFxStep(caseId);
        //判断当前方案流程到哪里
        if (step < 6) {
            request.setAttribute("step", step);
            return "suction/design";
        }
        Power powerTitle = powerDao.getCaseById(cId);
        OperateField operateField = operateFieldDao.getOperateField(caseId);
        SuctionDes suctionDes = suctionDesService.getOnceFanDes();
        request.setAttribute("powerTitle", powerTitle);
        request.setAttribute("suctionDes", suctionDes);
        request.setAttribute("caseId", caseId);
        request.setAttribute("operateField", operateField);
        request.setAttribute("message", request.getParameter("message"));
        return "suction/design";
    }

    @RequestMapping(value = "/saveValue", method = RequestMethod.POST)
    @ResponseBody
    public void saveValue(SuctionDes suctionDes) {
        suctionDesService.saveValueOfOnceFanDes(suctionDes);
    }

    @RequestMapping(value = "/calculateInit", method = RequestMethod.POST)
    @ResponseBody
    public void calculateInit() {
        sessionService.putSuctionDesInit(sessionService.getCaseId());
    }

    public void getInitSuction() {
        try {
            int caseId = sessionService.getCaseId();
            SuctionDes suctionDes = suctionDesService.getOnceFanDes();
            CalculateBurn burn = burnService.getCalculateBurn();
            Efficiency efficiency = efficiencyService.getEfficiency(caseId);
            AirGasParameter airGasParameter = airGasParameterService.getAirGasParameter(caseId);
            if (null != efficiency && null != airGasParameter) {
                Double E93 = efficiency.getE93();
                if (E93 != null) {
                    if (suctionDes.getE1() == null) {
                        suctionDes.setE1(E93);
                        suctionDesService.saveValueOfOnceFanDes(setParmValue("E1", E93));
                    }
                }
                Double E2 = airGasParameter.getE28();
                Double E3 = airGasParameter.getHearthOverOutAirCoefficient();
                Double E4 = airGasParameter.getAirPreheaterAirLeakageCoefficient();
                Double E5 = middleResultService.getMiddle(new MiddleResult(caseId, "E52")).getResult();
                if (E2 != null) {
                    if (suctionDes.getE2() == null) {
                        suctionDes.setE2(E2);
                        suctionDesService.saveValueOfOnceFanDes(setParmValue("E2", E2));
                    }
                }
                if (E3 != null) {
                    if (suctionDes.getE3() == null) {
                        suctionDes.setE3(E3);
                        suctionDesService.saveValueOfOnceFanDes(setParmValue("E2", E2));
                    }
                }
                if (E4 != null) {
                    if (suctionDes.getE4() == null) {
                        suctionDes.setE4(E4);
                        suctionDesService.saveValueOfOnceFanDes(setParmValue("E2", E2));
                    }
                }
                if (E5 != 0) {
                    if (suctionDes.getE5() == null) {
                        suctionDes.setE5(E5);
                        suctionDesService.saveValueOfOnceFanDes(setParmValue("E2", E2));
                    }
                }
            }
            if (null != burn) {
                Double E6_1 = burn.getE137();
                Double E6_2 = burn.getE70();
                if (E6_1 != null && E6_2 != null) {
                    if (suctionDes.getE6() == null) {
                        suctionDes.setE6(burn.getE137() + burn.getE70());
                        suctionDesService.saveValueOfOnceFanDes(setParmValue("E6", E6_1 + E6_2));
                    }
                }
                Double E9 = burn.getE155();
                if (E9 != null) {
                    if (suctionDes.getE9() == null) {
                        suctionDes.setE9(E9);
                        suctionDesService.saveValueOfOnceFanDes(setParmValue("E9", E9));
                    }
                }
                Double E7 = middleResultService.getMiddle(new MiddleResult(caseId, "RE104")).getResult();
                Double E8 = middleResultService.getMiddle(new MiddleResult(caseId, "RE248")).getResult();
                Double E10 = middleResultService.getMiddle(new MiddleResult(caseId, "RE318")).getResult();
                if (E7 != 0) {
                    if (suctionDes.getE7() == null) {
                        suctionDes.setE7(E7);
                        suctionDesService.saveValueOfOnceFanDes(setParmValue("E7", E7));
                    }
                }
                if (E8 != 0) {
                    if (suctionDes.getE8() == null) {
                        suctionDes.setE8(E8);
                        suctionDesService.saveValueOfOnceFanDes(setParmValue("E8", E8));
                    }
                }
                if (E10 != 0) {
                    if (suctionDes.getE10() == null) {
                        suctionDes.setE7(E10);
                        suctionDesService.saveValueOfOnceFanDes(setParmValue("E10", E10));
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private SuctionDes setParmValue(String id, Double value) {
        SuctionDes suctionDes = new SuctionDes();
        suctionDes.setParamName(id);
        suctionDes.setParamValue(value);
        return suctionDes;
    }

    @RequestMapping(value = "/getrecover", method = RequestMethod.GET)
    @ResponseBody
    public SuctionDes getSuctinDes() {
        getInitSuction();
        return suctionDesService.getOnceFanDes();
    }

    @RequestMapping(value = "/getInit", method = RequestMethod.POST)
    @ResponseBody
    public void getInit() {
        sessionService.putSuctionDesInit(sessionService.getCaseId());
    }

    @RequestMapping(value = "/Re-reference-boiler", method = RequestMethod.POST)
    @ResponseBody
    public SuctionDes reReference_boiler() {
        int caseId = sessionService.getCaseId();
        SuctionDes suctionDes = suctionDesService.getOnceFanDes();
        Efficiency efficiency = efficiencyService.getEfficiency(caseId);
        AirGasParameter airGasParameter = airGasParameterService.getAirGasParameter(caseId);
        if (null != efficiency && null != airGasParameter) {
            Double E93 = efficiency.getE93();
            if (E93 != null) {
                suctionDes.setE1(E93);
                suctionDesService.saveValueOfOnceFanDes(setParmValue("E1", E93));
            }
            Double E2 = airGasParameter.getE28();
            Double E3 = airGasParameter.getHearthOverOutAirCoefficient();
            Double E4 = airGasParameter.getAirPreheaterAirLeakageCoefficient();
            Double E5 = middleResultService.getMiddle(new MiddleResult(caseId, "E52")).getResult();
            if (E2 != null) {

                suctionDes.setE2(E2);
                suctionDesService.saveValueOfOnceFanDes(setParmValue("E2", E2));

            }
            if (E3 != null) {

                suctionDes.setE3(E3);
                suctionDesService.saveValueOfOnceFanDes(setParmValue("E3", E3));

            }
            if (E4 != null) {

                suctionDes.setE4(E4);
                suctionDesService.saveValueOfOnceFanDes(setParmValue("E4", E4));

            }
            if (E5 != 0) {

                suctionDes.setE5(E5);
                suctionDesService.saveValueOfOnceFanDes(setParmValue("E5", E5));

            }
        }


        return suctionDesService.getOnceFanDes();
    }

    @RequestMapping(value = "/Re-reference-burn", method = RequestMethod.POST)
    @ResponseBody
    public SuctionDes reReference_burn() {
        int caseId = sessionService.getCaseId();
        SuctionDes suctionDes = suctionDesService.getOnceFanDes();
        CalculateBurn burn = burnService.getCalculateBurn();
        if (null != burn) {
            Double E6_1 = burn.getE137();
            Double E6_2 = burn.getE70();
            if (E6_1 != null && E6_2 != null) {
                suctionDes.setE6(burn.getE137() + burn.getE70());
                suctionDesService.saveValueOfOnceFanDes(setParmValue("E6", E6_1 + E6_2));

            }
            Double E9 = burn.getE155();
            if (E9 != null) {

                suctionDes.setE9(E9);
                suctionDesService.saveValueOfOnceFanDes(setParmValue("E9", E9));

            }
            Double E7 = middleResultService.getMiddle(new MiddleResult(caseId, "RE104")).getResult();
            Double E8 = middleResultService.getMiddle(new MiddleResult(caseId, "RE248")).getResult();
            Double E10 = middleResultService.getMiddle(new MiddleResult(caseId, "RE318")).getResult();
            if (E7 != 0) {

                suctionDes.setE7(E7);
                suctionDesService.saveValueOfOnceFanDes(setParmValue("E7", E7));

            }
            if (E8 != null) {

                suctionDes.setE8(E8);
                suctionDesService.saveValueOfOnceFanDes(setParmValue("E8", E8));

            }
            if (E10 != null) {

                suctionDes.setE10(E10);
                suctionDesService.saveValueOfOnceFanDes(setParmValue("E10", E10));

            }
        }
        return suctionDesService.getOnceFanDes();
    }

    @RequestMapping(value = "suctionDesSave", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResponse suctionDesSave(SuctionDes suctionDes) {
        try {
            suctionDesService.suctionDesSave(suctionDes);
            return new AjaxResponse();
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof ValidateErrorException) {
                return new AjaxResponse(ReturnState.WARNING, e.getMessage());
            }
            return new AjaxResponse(ReturnState.ERROR, "请检查填写数据是否有误，操作失败");
        }
    }
}
